
package com.ttomato.game.framework.math;

import android.util.FloatMath;

public class Vector2 {
    public static final float TO_RADIANS = (1 / 180.0f) * (float) Math.PI;

    public static final float TO_DEGREES = (1 / (float) Math.PI) * 180.0f;

    public float mX, mY;

    public Vector2() {
    }

    public Vector2(float x, float y) {
        mX = x;
        mY = y;
    }

    public Vector2(Vector2 other) {
        mX = other.mX;
        mY = other.mY;
    }

    public Vector2 cpy() {
        return new Vector2(mX, mY);
    }

    public Vector2 set(float x, float y) {
        mX = x;
        mY = y;
        return this;
    }

    public Vector2 set(Vector2 other) {
        mX = other.mX;
        mY = other.mY;
        return this;
    }

    public Vector2 add(float x, float y) {
        mX += x;
        mY += y;
        return this;
    }

    public Vector2 add(Vector2 other) {
        mX += other.mX;
        mY += other.mY;
        return this;
    }

    public Vector2 sub(float x, float y) {
        mX -= x;
        mY -= y;
        return this;
    }

    public Vector2 sub(Vector2 other) {
        mX -= other.mX;
        mY -= other.mY;
        return this;
    }

    public Vector2 mul(float scalar) {
        mX *= scalar;
        mY *= scalar;
        return this;
    }

    public float len() {
        return FloatMath.sqrt(mX * mX + mY * mY);
    }

    public Vector2 nor() {
        float len = len();
        if (len != 0) {
            mX /= len;
            mY /= len;
        }
        return this;
    }

    public float angle() {
        float angle = (float) Math.atan2(mY, mX) * TO_DEGREES;
        if (angle < 0)
            angle += 360;
        return angle;
    }

    public Vector2 rotate(float angle) {
        float rad = angle * TO_RADIANS;
        float cos = FloatMath.cos(rad);
        float sin = FloatMath.sin(rad);

        float newX = mX * cos - mY * sin;
        float newY = mX * sin + mY * cos;

        mX = newX;
        mY = newY;

        return this;
    }

    public float dist(Vector2 other) {
        float disX = other.mX - mX;
        float disY = other.mY - mY;

        return FloatMath.sqrt(disX * disX + disY * disY);
    }

    public float dist(float x, float y) {
        float disX = x - mX;
        float disY = y - mY;

        return FloatMath.sqrt(disX * disX + disY * disY);
    }

    public float distSquared(Vector2 other) {
        float distX = mX - other.mX;
        float distY = mY - other.mY;
        return distX * distX + distY * distY;
    }

    public float distSquared(float x, float y) {
        float distX = mX - x;
        float distY = mY - y;
        return distX * distX + distY * distY;
    }
}
